package net.example.lesson6.jaxb;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.io.File;
import java.util.List;

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlBean {
    @XmlElement(name = "name")
    private String name;
    @XmlElement(name = "Query")
    private Action query;
    @XmlElement(name = "Update")
    private Action update;


    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Action {
        @XmlElement(name = "name")
        private String name;
        @XmlAnyElement(lax=true, value = BioHandler.class)
        private String test;
        @XmlElement(name = "url")
        private String url;

        @XmlElement(name = "add")
        @XmlElementWrapper(name = "adds")
        private List<Add> adds;

        public String getName() {
            return name;
        }

        public void setName(final String name) {
            this.name = name;
        }

        public String getTest() {
            return test;
        }

        public void setTest(final String test) {
            this.test = test;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(final String url) {
            this.url = url;
        }

        public List<Add> getAdds() {
            return adds;
        }

        public void setAdds(final List<Add> adds) {
            this.adds = adds;
        }
    }

    public static class Add {
        @XmlAttribute
        private String from;
    }



    public static void main(String[] args) {
        try {
            XmlBean xb = new XmlBean();
            xb.query = new Action();
            xb.update = new Action();

            File file = new File("/Users/kevinyang/Downloads/test.xml");

            JAXBContext jaxbContext = JAXBContext.newInstance(XmlBean.class);
//            Marshaller marshaller = jaxbContext.createMarshaller();
//            marshaller.setProperty("jaxb.encoding", "Unicode");
//            //格式化输出，即按标签自动换行，否则就是一行输出
//            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//            //设置编码（默认编码就是utf-8）
//            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
//            //是否省略xml头信息，默认不省略（false）
//            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
//            marshaller.marshal(xb, file);

            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

            XmlBean xb1 = (XmlBean) unmarshaller.unmarshal(file);

            System.out.println("unmarshall finished");
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}
